package com.chap16.threadlocal;

import java.util.UUID;

/**
 * <p>
 * <code>CallChainDemo</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-04-26 9:31
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class CallChainDemo {
    private static final ThreadLocal<String> tid = new ThreadLocal<>();

    public static void main(String[] args) {
        try{

            tid.set(UUID.randomUUID().toString());
            ServiceA serviceA = new ServiceA();
            serviceA.doSomething();
        }finally {
            tid.remove();
        }
    }

    static class ServiceA{
        void doSomething(){
            String id = tid.get();
            System.out.println("ServiceA:"+id);
            ServiceB serviceB = new ServiceB();
            serviceB.doSomething();

        }
    }

    static class ServiceB{
        void doSomething(){
            String id = tid.get();
            System.out.println("ServiceB:"+id);

            ServiceC serviceC = new ServiceC();
            serviceC.doSomething();

        }
    }

    static class ServiceC{
        void doSomething(){
            String id = tid.get();
            System.out.println("ServiceC:"+id);

        }
    }
}
